﻿using VhaBot.Net;
using VhaBot.Net.Events;
using System;
using System.Threading;

namespace Manso.cBot.Common
{
    /// <summary>
    /// This object wrapper a friend add and remove operation to retrieve character information. See Who command in Core module for more information.
    /// </summary>
    public class Who
    {
        private FriendStatusEventArgs _friendArgs;
        private string _characterName;
 
        public FriendStatusEventArgs WhoIs(string characterName)
        {
            _characterName = characterName;
            cBotContext.Instance.FriendStatusEvent += Instance_FriendStatusEvent;
            cBotContext.Instance.AddFriend(characterName);
            DateTime requestedAt = DateTime.Now;
            while (_friendArgs == null && (DateTime.Now - requestedAt).TotalSeconds < 2)
            {
                Thread.Sleep(100);
            }
            cBotContext.Instance.FriendStatusEvent -= Instance_FriendStatusEvent;
            cBotContext.Instance.RemoveFriend(characterName);
            return _friendArgs;
        }

        void Instance_FriendStatusEvent(Chat chat, FriendStatusEventArgs e)
        {
            if (_characterName.ToLower().Equals(e.Character.ToLower())) _friendArgs = e;
        }
    }
}
